﻿using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using zhangmenren.IService;
using zhangmenren.Models;
using zhangmenren.Models.Enums;
using zhangmenren.Models.help;
using zhangmenren.Untils;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.Web.CodeGeneration;

namespace zhangmenren.Controllers
{
	public class UserController : Controller
	{
		private IUserService userService;
		private IApplyService applyService;
		private ICensusService censusService;
		private IWorksService worksService;
        private IRealService realService;
        private IOrderService orderService;
		public UserController(IRealService _realService, IOrderService _orderService , IWorksService _worksService, ICensusService _censusService, IApplyService _applyService, IUserService _userService)
		{
			userService = _userService;
			applyService = _applyService;
			censusService = _censusService;
			worksService = _worksService;
            realService = _realService;
            orderService = _orderService;
        }
		/// <summary>
		/// 用户中心
		/// </summary>
		/// <returns>The index.</returns>
		public async Task<IActionResult> Index()
		{
            ///return Redirect("/Login/Index");
            var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Redirect("/Login/Index");
			}
			var entity = await userService.FindById(user.Id);
			if (string.IsNullOrEmpty(entity.UserName))
			{
				return Redirect("/Login/Index");
			}


			ViewBag.DianZhan = 0;
			ViewBag.ShouChang = 0;
			ViewBag.GuanZhu = 0;
			ViewBag.GouMai = 0;
			var census = await GetCensus(user.Id);
			if (census.Any())
			{
				ViewBag.ShouChang = census.FindAll(p => p.Type == 0)?.Count;
				ViewBag.DianZhan = census.FindAll(p => p.Type ==1)?.Count;
				
				ViewBag.GuanZhu = census.FindAll(p => p.Type == 2)?.Count;
				ViewBag.GouMai = census.FindAll(p => p.Type == 3)?.Count;
			}


			ViewBag.WenDang = 0;
			ViewBag.ShiPin = 0;
			if (entity.ZMRID>0)
			{
				var works = await GetWorks(entity.ZMRID);

				if (works.Any())
				{
					ViewBag.WenDang = works.FindAll(p => p.Type == WorkType.WENDANG)?.Count;
					ViewBag.ShiPin = works.FindAll(p => p.Type == WorkType.PTSP)?.Count;
				}
			}


            ViewBag.RenZheng = await GetReal(entity.Id);






            return View(entity);
		}

		public IActionResult MyWork()
		{
			return View();
		}
		/// <summary>
		/// 作者控制器，带评论的
		/// </summary>
		/// <returns>The author.</returns>
		public IActionResult Author()
		{
			return View();
		}

		public async Task<IActionResult> MyWorks()
		{
			var user = QTCookie.GetCookie<UserCook>(HttpContext);
			if (user == null)
			{
				return Redirect("/Login/Index");
			}

            var orders = await orderService.FindByUserId(user.Id);
            ViewBag.ZhiChu = 0;

            if (orders.Any())
            {
                ViewBag.ZhiChu = orders.Sum(p => p.Price);
            }

			return View(orders.ToList());
		}



		public async Task<IActionResult> ShouRu()
		{

			return View();
		}

		


		#region 获取数据

		/// <summary>
		/// 获取购买关注等列表
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		public async Task<List<CensusEntity>> GetCensus(int userId)
		{
			var list = await censusService.FindByUserId(userId);
			return list.ToList();
		}

		/// <summary>
		/// 获取该用户的作品
		/// </summary>
		/// <param name="zmId"></param>
		/// <returns></returns>
		public async Task<List<WorksEntity>> GetWorks(int zmId)
		{
			var list = await worksService.FindByZMId(zmId);
			return list.ToList();
		}
       

        public async Task<int> GetReal(int userid) {

            var real = await realService.FindByUser(userid);
            if (real==null||string.IsNullOrEmpty(real.Name))
            {
                return  -1;
            }
            else
            {
                return real.State;
            }

        }


        #endregion


        [HttpPost]
        public JsonResult DelCookie()
        {
            QTCookie.DeleteCookie(HttpContext);
            return Json(new { state="ok"});
        }

    }
}
